package xyz.syyrjx.blog.controller.tool.shakeRice;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import xyz.syyrjx.blog.entity.Food;
import xyz.syyrjx.blog.entity.Result;
import xyz.syyrjx.blog.service.tools.FoodService;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/tools/shakeRice")
public class ShakeRiceController {
    @Resource
    private FoodService service;

    @RequestMapping("/addFood")
    @ResponseBody
    public Result writeFood(Food foodIn) {
        if (foodIn.getFood() == null || foodIn.getFood().length() == 0) {
            return Result.fail("请输入名称");
        }
        Food food = service.getFoodByName(foodIn);
        if (food == null) {
            service.addFood(foodIn);
            return Result.success("添加成功");
        }
        return Result.success(food.getFood() + "已存在");
    }

    @RequestMapping("/getAllName")
    @ResponseBody
    public Result getAllName() {
        List<String> foodNameList = service.getAllName();
        return Result.success(foodNameList);
    }
}
